// pages/skcard/changecard.js
var utl = require('../../utils/util.js');
var city = require('../../utils/city.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name:'',    //持卡人
    cardN:'', //卡号
    tel:'', //手机号
    showbank: false,
    banks: [],
    bindex: 0,
    citys: city.mtData().list,
    pindex: 0,
    cindex: -100,
    showp: false,
    showc: false,
    userDic:{},
    cantouch:false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var detail = options.jpram;
    var cjson = JSON.parse(detail);
    this.setData({
      userDic:cjson,
    })

    this.checkbank();
  },
  //确认修改
  changeAction:function(){
    const mob = this.data.tel;
    const cardno = this.data.cardN;
    const cardN = this.data.name;

    const bindex = this.data.bindex;
    const banks = this.data.banks;
    var bank = banks[bindex];
    var bank_jc = bank.value; //银行简称
    var bank_name = bank.text; //银行名称
    console.log('银行简称' + bank_jc);

    const pindex = this.data.pindex;
    const citys = this.data.citys;
    const cindex = this.data.cindex;
    var province = citys[pindex];
    var province_name = province.text; //省名称
    var province_code = province.value; //省CODE
    console.log('省会名称' + province_name);
    var provice_citys = province.children; //省辖下的市区
    var city_name = '';
    var city_code = '';
    if (province_name.search("市") != -1) {
      city_name = province_name;
      city_code = province_code;
    } else {
      var city = provice_citys[cindex];
      city_name = city.text;
      city_code = city.value;
    }
    var usdata = this.data.userDic;
    if (mob == '' || cardno == '' || cardN == '' || bank_name == '' || city_name == '' || usdata == ''){
      wx.showToast({
        title: '提交数据不全',
        icon:'none',
        duration:2000,
      })
      return;
    }
    utl.serverToken('/epay_apps/bankCard/updateUserInfoE0600', { 
      'mobile': mob,
      'accountNo': cardno,
      'accountName': cardN,
      'bankLeaveMobile': mob,
      'provinceName': province_name,
      'provinceId': province_code,
      'cityName': city_name,
      'cityId': city_code,
      'openBankName': bank_name,
      'bankCode': bank_jc,
      'issuerCodeName': bank_name,
      'issuerCode': '102290002756',
      'idNo': usdata.crpIdenNumber,
      'name': usdata.userName,
    }, (err, res) => {
      wx.hideLoading();
      if (err) {
        wx.showToast({
          title: err,
          icon: 'none',
          duration: 2000
        })
        return;
      }
      wx.showModal({
        title: '修改成功',
        content: '返回个人中心界面',
        success(res) {
          if (res.confirm) {
           wx.navigateBack({
             delta:2,
           })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }

      })
    })
  },
  //查询银行
  checkbank: function () {
    utl.serverToken('/epay_apps/bankCard/searchBankListB0700', {}, (err, res) => {
      wx.hideLoading();
      if (err) {
        wx.showToast({
          title: err,
          icon: 'none',
          duration: 2000
        })
        return;
      }
      var rdata = res.response.bankList;
      this.setData({
        banks: rdata,
      })
    })
  },
  //选择银行
  bindBankPickerChange: function (e) {
    this.setData({
      bindex: e.detail.value,
      showbank: true
    })
  },
  //绑定省
  bindProvincePickerChange: function (e) {
    this.setData({
      pindex: e.detail.value,
      showp: true,
      cindex: 0,
      cantouch: true
    })
  },
  //选择市区
  bindCityChange: function (e) {
    this.setData({
      cindex: e.detail.value,
      showc: true,
    

    })
  },
  //预留手机号
  commitTel:function(e){
    this.setData({
      tel:e.detail.value
    })
  },
  //持卡人
  username:function(e){
      this.setData({
        name:e.detail.value,
      })
  },
  //卡号
  commitcard:function(e){
    this.setData({
      cardN:e.detail.value,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})